<?php
/**
 * Class name  is ConfigBaseController * @package backend\modules\common\controllers;
 * @author  Womtech  email:chareler@163.com
 * @DateTime 2020-04-15 01:50 
 */

namespace backend\modules\common\controllers;

use Yii;
use common\models\config\ConfigBase;
use common\models\config\ConfigBaseSearch;
use backend\controllers\BaseController;
use yii\web\NotFoundHttpException;
use yii\filters\VerbFilter;
use \yii\web\Response;
use yii\helpers\Html;
use common\utils\ToolUtil;


/**
 * ConfigBaseController implements the CRUD actions for ConfigBase model.
 */
class ConfigBaseController extends BaseController
{
    public function actions()
    {
        return [
            
        ];
    }

    /**
     * @inheritdoc
     */
    public function behaviors()
    {
        return [
            'verbs' => [
                'class' => VerbFilter::className(),
                'actions' => [
                    'delete' => ['post'],
                    'bulkdelete' => ['post'],
                ],
            ],
        ];
    }

    /**
     * Lists all ConfigBase models.
     * @return mixed
     */
    public function actionIndex()
    {    
        return $this->render('index', [

        ]);
    }

    /**
     * 获取baseconfig的所有数据
     *
     * @return void
     */
    public function actionGetBaseConfigList(){       
        
        $searchModel = new ConfigBaseSearch();
        $searchModel->scenario = 'search';
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());
        if($searchModel->hasErrors()){
            $return=ToolUtil::returnAjaxMsg(false,'参数有误', ['errors'=>$searchModel->errors]);
            return $this->asJson($return);
        }

        
        $count = $dataProvider->getTotalCount();        
        return $this->asJson([
            'code' => 0,
            'msg' => '',
            'count' => (int)$count,
            'data' => $dataProvider->getModels(),
        ]);

        


        /* $query = ConfigBase::find()->filterWhere([]);
        return $this->getJqTableData($query); */

       /*  $authName = $this->post('authName');
        $start = $this->post('start');
        $end = $this->post('end');
        $query = AuthItem::find()->filterWhere(['AND',['type' => 1],['like','description',$authName]]);
        if(!empty($start)){
            $startTime = strtotime($start. " 00:00:00");
            $query->andWhere(['>=','created_at',$startTime]);
        }
        if(!empty($end)){
            $endTime = strtotime($end. " 23:59:59");
            $query->andWhere(['<=','created_at',$endTime]);
        }
        $this->sidx = 'order_sort DESC,created_at';
        return $this->getJqTableData($query); */


    }
    public function actionGetBaseConfigListbak(){
        /* $arrFilterWhere =[];
        if(empty(yii::$app->request->post("page"))){
            $page= yii::$app->request->post("page");
            $arrFilterWhere[]=[];
        }
        if(empty(yii::$app->request->post("limit"))){
            $limit= yii::$app->request->post("limit");
        } */



        //$authName = $this->post('authName');
        /* $start = $this->post('start');
        $end = $this->post('end');
        $title = $this->post('title'); */


        $searchModel = new ConfigBaseSearch();
        $searchModel->scenario = 'search';
        $searchModel->load(Yii::$app->request->getBodyParams(),'');
        if(!$searchModel->validate(['start','end'])){
            $return=ToolUtil::returnAjaxMsg(false,'参数有误', ['errors'=>$searchModel->errors]);
            //$return=ToolUtil::returnAjaxMsg(false,$searchModel->errors, ['errors'=>$searchModel->errors]);
            return $this->asJson($return);
            /* return $this->asJson([
                'code' => 0,
                'msg' => '',
                'count' => 0,
                'data' => $searchModel->errors,
            ]); */

        }
        
        $dataProvider = $searchModel->search(Yii::$app->request->getBodyParams());

        $count = $dataProvider->getTotalCount();
        
        return $this->asJson([
            'code' => 0,
            'msg' => '',
            'count' => (int)$count,
            'data' => $dataProvider->getModels(),
        ]);


        /* $query = ConfigBase::find()->filterWhere([]);
        return $this->getJqTableData($query); */

       /*  $authName = $this->post('authName');
        $start = $this->post('start');
        $end = $this->post('end');
        $query = AuthItem::find()->filterWhere(['AND',['type' => 1],['like','description',$authName]]);
        if(!empty($start)){
            $startTime = strtotime($start. " 00:00:00");
            $query->andWhere(['>=','created_at',$startTime]);
        }
        if(!empty($end)){
            $endTime = strtotime($end. " 23:59:59");
            $query->andWhere(['<=','created_at',$endTime]);
        }
        $this->sidx = 'order_sort DESC,created_at';
        return $this->getJqTableData($query); */


    }

    /**
     * Displays a single ConfigBase model.
     * @param integer $id
     * @return mixed
     */
    public function actionView($id)
    {   
        
    }

    /**
     * Creates a new ConfigBase model.
     * For ajax request will return json object
     * and for non-ajax request if creation is successful, the browser will be redirected to the 'view' page.
     * @return mixed
     */
    public function actionCreate()
    {
        $request = Yii::$app->request;
        
         


        //如果是表单get提交,如果有参数id,则是更改,没有则是新增
        if($request->isGet){
            $id = $this->get('id');
            if(empty($id)){
                $model = new ConfigBase();  
                return $this->render('create', [
                ]);
            }else{
                $model = $this->findModel($id);
                return $this->render('create', [
                    'model' => $model,
                ]);

            }
        }

        if($request->isPost){
            //$id = yii::$app->request->post()["ConfigBase"]["id"];
            $id = yii::$app->request->post("ConfigBase")["id"];

            //如果没有id,说明是新增
            if(empty($id)){
                $model = new ConfigBase();  
                if ($model->load($request->post()) && $model->save()) {                   
                    return ToolUtil::returnAjaxMsg(true,'添加成功');
                } 

            }else{
                $model = $this->findModel($id);
                if ($model->load($request->post()) && $model->save()){
                    $return=ToolUtil::returnAjaxMsg(true,'修改成功');
                }else{
                    $return=ToolUtil::returnAjaxMsg(false,'修改失败');
                }
                return $this->asJson($return);
            }

        }


        /* $model = new ConfigBase();   
            if ($model->load($request->post()) && $model->save()) {
                //return $this->redirect(['view', 'id' => $model->id]);
                $return = (!empty($id)) ? ToolUtil::returnAjaxMsg(true,'更新成功') : ToolUtil::returnAjaxMsg(true,'添加成功');
                return $return;
            }  */   
        //否则就显示添加
        
        
        
            /* return $this->render('create', [
                //'model' => $model,
            ]); */
        



    }

    /**
     * Updates an existing ConfigBase model.
     * For ajax request will return json object
     * and for non-ajax request if update is successful, the browser will be redirected to the 'view' page.
     * @param integer $id
     * @return mixed
     */
    public function actionUpdate()
    {
        
        $request = Yii::$app->request;
        $id = (int)$request->post("id");
        $model = $this->findModel($id); 
        

        if ($model->load($request->post(),'') && $model->save()) {
            return ToolUtil::returnAjaxMsg(true,'修改成功');


        } else {
            return ToolUtil::returnAjaxMsg(true,'修改失败');
        }




    }

    /**
     * Delete an existing ConfigBase model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id,可以是单个id,也可以是多个id的组合字符串,如1,3,4
     * @return mixed
     */
    public function actionDelete()
    {
        //
        //return "123";

        $request = Yii::$app->request;
        $id = $request->post("id");  //数组格式
        $type=$request->post("type","hard");     
        $result = false;
        $arrIds = explode(",",$id);
        foreach($arrIds as $item){
            $model = $this->findModel($item);
            switch($type){
                case 'hard':
                    $result =  $model->delete();
                    break;
                case 'soft':
                    $model->status = -1;
                    $result = $model->save();
                    break;
                default:
                    break;
            }

            if(!($result)){
                return $this->asJson(ToolUtil::returnAjaxMsg(false,'删除失败'));
            }

        }
        

        $return=($result) ? ToolUtil::returnAjaxMsg(true,'删除成功') : ToolUtil::returnAjaxMsg(false,'删除失败');
        return $this->asJson($return);

        /* if($request->isAjax){
            Yii::$app->response->format = Response::FORMAT_JSON;
            return ['forceClose' => true,'forceReload' => '#crud-datatable-pjax'];
        }else{
            return $this->redirect(['index']);
        } */
    }

     /**
     * Delete multiple existing ConfigBase model.
     * For ajax request will return json object
     * and for non-ajax request if deletion is successful, the browser will be redirected to the 'index' page.
     * @param integer $id
     * @return mixed
     */
    public function actionBulkdelete($type = 'hard')
    {        
       
    }


    /**
     * Finds the ConfigBase model based on its primary key value.
     * If the model is not found, a 404 HTTP exception will be thrown.
     * @param integer $id
     * @return ConfigBase the loaded model
     * @throws NotFoundHttpException if the model cannot be found
     */
    protected function findModel($id)
    {
        if (($model = ConfigBase::findOne($id)) !== null) {
            return $model;
        } else {
            throw new NotFoundHttpException('The requested page does not exist.');
        }
    }


}
